package com.neusoft.neuiotms.dcam.fixed.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcam.fixed.converter.InventoryPlanConverter;
import com.neusoft.neuiotms.dcam.fixed.dto.InventoryPlanDTO;
import com.neusoft.neuiotms.dcam.fixed.service.InventoryPlanService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "盘点计划管理")
@RestController
@RequestMapping(value = "/api/w/fixed/inventory/plans")
public class InventoryPlanController {

    @Autowired
    private InventoryPlanService inventoryPlanService;
    @Autowired
    private InventoryPlanConverter inventoryPlanConverter;

    @ApiOperation("查询所有盘点计划")
    @GetMapping
    public ResultListDTO<InventoryPlanDTO> index(final Searchable searchable) {
        final List<InventoryPlanDTO> DTOs = this.inventoryPlanService.findAll(searchable);
        final ResultListDTO<InventoryPlanDTO> result = ResultListDTO.success(DTOs);
        return result;
    }

    @ApiOperation("分页查询所有盘点计划")
    @GetMapping("/p")
    public PageResultDTO<InventoryPlanDTO> search(final Pageable pageable, final Searchable searchable) {
        return PageResultDTO.success(this.inventoryPlanService.search(pageable, searchable));
    }

    @ApiOperation("查询已创建盘点计划")
    @GetMapping("/status/{status}")
    public ResultListDTO<InventoryPlanDTO> status(@PathVariable final String status) {
        final List<InventoryPlanDTO> DTOs = this.inventoryPlanService.findByStatus(status);
        final ResultListDTO<InventoryPlanDTO> result = ResultListDTO.success(DTOs);
        return result;
    }

    @ApiOperation("创建盘点计划")
    @PostMapping
    public ResultDTO<InventoryPlanDTO> create(@RequestBody final InventoryPlanDTO dto, final UserBean user) {
        final InventoryPlanDTO result = this.inventoryPlanService.create(this.inventoryPlanConverter.merge(dto, user));
        return ResultDTO.success(result);
    }

    @ApiOperation("盘点计划详情")
    @GetMapping("/{id}")
    public ResultDTO<InventoryPlanDTO> details(@PathVariable final Long id) {
        final InventoryPlanDTO dto = this.inventoryPlanService.details(id);
        return ResultDTO.success(dto);
    }

    @ApiOperation("删除盘点计划")
    @DeleteMapping("/{id}")
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.inventoryPlanService.delete(id);
        return ResultDTO.success();
    }

    @ApiOperation("修改盘点计划")
    @PutMapping("/{id}")
    public ResultDTO<InventoryPlanDTO> update(@PathVariable final Long id,
            @RequestBody final InventoryPlanDTO dto) {
        final InventoryPlanDTO result = this.inventoryPlanService.update(id, dto);
        return ResultDTO.success(result);
    }

    @ApiOperation("下发计划")
    @PutMapping("/issue/{id}")
    public ResultDTO<Void> issue(@PathVariable final Long id) {
        this.inventoryPlanService.issue(id);
        return ResultDTO.success();
    }

    @ApiOperation("完成计划")
    @PutMapping("/doneplan/{id}")
    public ResultDTO<Void> donePlan(@PathVariable final Long id) {
        this.inventoryPlanService.donePlan(id);
        return ResultDTO.success();
    }

    @ApiOperation("上传结果到资产")
    @PostMapping("/syndata/{ids}/rule/{rule}")
    public ResultDTO<Void> synchronousData(@PathVariable final String ids, @PathVariable final String rule) {
        this.inventoryPlanService.synchronousData(ids, rule);
        return ResultDTO.success();
    }
}
